package com.jt.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.service.UserService;
import com.jt.utils.CookieUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 为用户提供json数据
 * @author dc
 */
@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;



	/**
	 * 实现用户数据的校验,跨域访问
	 * @param param 校验的数据
	 * @param type 类型
	 * @param callback 回调函数名称
	 * @return 封装好的回调数据
	 */
	@RequestMapping("/check/{param}/{type}")
	public JSONPObject checkUser(@PathVariable String param, @PathVariable Integer type,String callback){
		JSONPObject jsonpObject;
		boolean flag=userService.checkUser(param,type);
		SysResult sysResult=SysResult.success(flag);
		jsonpObject = new JSONPObject(callback,sysResult);
		return jsonpObject;
	}

	/**
	 * 业务:完成用户信息的跨域获取
	 * url : "http://sso.jt.com/user/query/" + _ticket
	 * @param ticket uuid,redis中用户信息的key
	 * @param callback jsonp回调函数
	 * @return jsonp封装数据 callback({SysResult})
	 */
	@RequestMapping("/query/{ticket}")
	public JSONPObject findUserByTicket(@PathVariable String ticket, String callback, HttpServletResponse response){
		String userJson =userService.findUserByTicket(ticket);
		if (StringUtils.isEmpty(userJson)){
			//缓存中没有数据,证明ticket信息有问题,应该删除cookie
			CookieUtil.deleteCookie(response,"JT_TICKET","jt.com","/");
			return new JSONPObject(callback,SysResult.fail());
		}
		return new JSONPObject(callback,SysResult.success(userJson));
	}




}
